/*
 * Copyright (C) 2020 yixun All Rights Reserved.
 * 
 * ReportCopyRecordServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.ReportCopyRecordMapper;
import com.yixun.qfbao.model.ReportCopyRecord;
import com.yixun.qfbao.model.dto.ReportCopyRecordDto;
import com.yixun.qfbao.service.ReportCopyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

@Service("reportCopyRecordService")
public class ReportCopyRecordServiceImpl implements ReportCopyRecordService {
	
	@Autowired
	private ReportCopyRecordMapper mapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<ReportCopyRecord> getPageModel(ReportCopyRecordDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<ReportCopyRecord> rows = mapper.list(entity,begin,end);
	    Page<ReportCopyRecord> msr = new Page<ReportCopyRecord>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<ReportCopyRecord> selectListByDto(ReportCopyRecordDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public ReportCopyRecord selectByDto(ReportCopyRecordDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param id 搜索条件
	 * @return 分页记录
	 */
	public ReportCopyRecord selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public ReportCopyRecordDto create(ReportCopyRecordDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(ReportCopyRecordDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(ReportCopyRecordDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(ReportCopyRecordDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public ReportCopyRecord selectBy(ReportCopyRecord entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	@Override
	public List<ReportCopyRecordDto> selectCopyCountByReport(ReportCopyRecordDto entity) {
		return mapper.selectCopyCountByReport(entity);
	}
}
